﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Configuration;
using Common.eConnect.Communicator.DynamicProxy;

namespace Common.eConnect.Communicator
{
    public class eConnectCommunicator
    {
        internal readonly string DynamiceConectWcfURI = "http://localhost/Microsoft/Dynamics/GP/eConnect/mex";

        private DynamicProxyFactory factory = null;
        private bool EnableDebug = false;
        internal string DebugOutPutDirectory = string.Empty;

        public eConnectCommunicator()
        {
            factory = new DynamicProxyFactory(DynamiceConectWcfURI);
            EnableDebug = ConfigurationSettings.AppSettings["Debug"] == null ? false : ConfigurationSettings.AppSettings["Debug"].Equals("1");
            DebugOutPutDirectory = ConfigurationSettings.AppSettings["DebugOutPutDirectory"];

            if (EnableDebug)
            {
                if (String.IsNullOrEmpty(DebugOutPutDirectory))
                {
                    DebugOutPutDirectory = Environment.CurrentDirectory + "\\Communicator\\";

                    var o = Directory.CreateDirectory(DebugOutPutDirectory);

                    if (o.Exists == false)
                        throw new DirectoryNotFoundException(DebugOutPutDirectory, new Exception("Directory could not be created. Check user permissions."));
                }
                else
                {
                    if (Directory.Exists(DebugOutPutDirectory) == false)
                        throw new DirectoryNotFoundException(DebugOutPutDirectory);

                    if (DebugOutPutDirectory.LastIndexOf('\\') != (DebugOutPutDirectory.Length - 1))
                        DebugOutPutDirectory += "\\";
                }
            }
        }

        public bool CreateEntity(string connectionString, string xml)
        {
            if (EnableDebug)
                File.WriteAllText(string.Format("{1}{2}.{0}.xml", DateTime.Now.ToFileTime(), DebugOutPutDirectory, "CreateEntity"), xml);

            DynamicProxy.DynamicProxy DynCreateEntity = factory.CreateProxy("eConnect");

            return (bool)DynCreateEntity.CallMethod("CreateEntity", connectionString, xml);
        }

        public string CreateTransactionEntity(string connectionString, string xml)
        {
            if (EnableDebug)
                File.WriteAllText(string.Format("{1}{2}.{0}.xml", DateTime.Now.ToFileTime(), DebugOutPutDirectory, "CreateTransactionEntity"), xml);

            DynamicProxy.DynamicProxy DynCreateEntity = factory.CreateProxy("eConnect");

            return (string)DynCreateEntity.CallMethod("CreateTransactionEntity", connectionString, xml);
        }

        public bool DeleteEntity(string connectionString, string xml)
        {
            if (EnableDebug)
                File.WriteAllText(string.Format("{1}{2}.{0}.xml", DateTime.Now.ToFileTime(), DebugOutPutDirectory, "DeleteEntity"), xml);

            DynamicProxy.DynamicProxy DynCreateEntity = factory.CreateProxy("eConnect");

            return (bool)DynCreateEntity.CallMethod("DeleteEntity", connectionString, xml);
        }

        public bool DeleteTransactionEntity(string connectionString, string xml)
        {
            if (EnableDebug)
                File.WriteAllText(string.Format("{1}{2}.{0}.xml", DateTime.Now.ToFileTime(), DebugOutPutDirectory, "DeleteTransactionEntity"), xml);

            DynamicProxy.DynamicProxy DynCreateEntity = factory.CreateProxy("eConnect");

            return (bool)DynCreateEntity.CallMethod("DeleteTransactionEntity", connectionString, xml);
        }

        public string GetEntity(string connectionString, string xml)
        {
            if (EnableDebug)
                File.WriteAllText(string.Format("{1}{2}.{0}.xml", DateTime.Now.ToFileTime(), DebugOutPutDirectory, "GetEntity"), xml);

            DynamicProxy.DynamicProxy DynCreateEntity = factory.CreateProxy("eConnect");

            return (string)DynCreateEntity.CallMethod("GetEntity", connectionString, xml);
        }

        public string GetNextGLJournalEntryNumber(object incdec, string databaseConnectionString)
        {
            DynamicProxy.DynamicProxy DynCreateEntity = factory.CreateProxy("eConnect");

            return (string)DynCreateEntity.CallMethod("GetNextGLJournalEntryNumber", incdec, databaseConnectionString);
        }

        public string GetNextIVNumber(object incdec, object documentType, string databaseConnectionString)
        {
            DynamicProxy.DynamicProxy DynCreateEntity = factory.CreateProxy("eConnect");

            return (string)DynCreateEntity.CallMethod("GetNextGLJournalEntryNumber", incdec, documentType, databaseConnectionString);
        }

        public string GetNextPONumber(object incdec, string databaseConnectionString)
        {
            DynamicProxy.DynamicProxy DynCreateEntity = factory.CreateProxy("eConnect");

            return (string)DynCreateEntity.CallMethod("GetNextPONumber", incdec, databaseConnectionString);
        }

        public string GetNextPOPReceiptNumber(object incdec, string databaseConnectionString)
        {
            DynamicProxy.DynamicProxy DynCreateEntity = factory.CreateProxy("eConnect");

            return (string)DynCreateEntity.CallMethod("GetNextPOPReceiptNumber", incdec, databaseConnectionString);
        }

        public string GetNextRMNumber(object incdec, object documentType, string databaseConnectionString)
        {
            DynamicProxy.DynamicProxy DynCreateEntity = factory.CreateProxy("eConnect");

            return (string)DynCreateEntity.CallMethod("GetNextRMNumber", incdec, documentType, databaseConnectionString);
        }

        public string GetNextSOPNumber(object incdec, object documentType, string databaseConnectionString)
        {
            DynamicProxy.DynamicProxy DynCreateEntity = factory.CreateProxy("eConnect");

            return (string)DynCreateEntity.CallMethod("GetNextSOPNumber", incdec, documentType, databaseConnectionString);
        }

        public string GetPMNextVoucherNumber(object incdec, string databaseConnectionString)
        {
            DynamicProxy.DynamicProxy DynCreateEntity = factory.CreateProxy("eConnect");

            return (string)DynCreateEntity.CallMethod("GetPMNextVoucherNumber", incdec, databaseConnectionString);
        }

        public string RollBackDocuments(object[] documentNumberCollection, string databaseConnectionString)
        {
            DynamicProxy.DynamicProxy DynCreateEntity = factory.CreateProxy("eConnect");

            return (string)DynCreateEntity.CallMethod("RollBackDocuments", documentNumberCollection, databaseConnectionString);
        }

        public bool UpdateEntity(string connectionString, string xml)
        {
            if (EnableDebug)
                File.WriteAllText(string.Format("{1}{2}.{0}.xml", DateTime.Now.ToFileTime(), DebugOutPutDirectory, "UpdateEntity"), xml);

            DynamicProxy.DynamicProxy DynCreateEntity = factory.CreateProxy("eConnect");

            return (bool)DynCreateEntity.CallMethod("UpdateEntity", connectionString, xml);
        }

        public bool UpdateTransactionEntity(string connectionString, string xml)
        {
            if (EnableDebug)
                File.WriteAllText(string.Format("{1}{2}.{0}.xml", DateTime.Now.ToFileTime(), DebugOutPutDirectory, "UpdateTransactionEntity"), xml);

            DynamicProxy.DynamicProxy DynCreateEntity = factory.CreateProxy("eConnect");

            return (bool)DynCreateEntity.CallMethod("UpdateTransactionEntity", connectionString, xml);
        }
    }
}
